package io.renren.sf.controller;

import java.io.File;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.Constant;
import io.renren.sf.entity.CbBookEntity;
import io.renren.sf.entity.CbTypefaceEntity;
import io.renren.sf.service.FixedInformationDicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import io.renren.sf.entity.CbStandardBrushEntity;
import io.renren.sf.service.CbStandardBrushService;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author cyl_txw
 * @email cyl_txw@gmail.com
 * @date 2021-04-26 16:00:55
 */
@RestController
@RequestMapping("sf/cbstandardbrush")
public class CbStandardBrushController {
    @Autowired
    private CbStandardBrushService cbStandardBrushService;

    @Autowired
    private FixedInformationDicService fixedInformationDicService;

    @GetMapping("/getBzbfList")
    public R getBzbfList(@RequestParam Map<String, Object> params){
        long page = Long.parseLong((String) params.get("page"));
        long limit = Long.parseLong((String) params.get("limit"));
        Page<CbStandardBrushEntity> bzbfPage = new Page<>(page, limit);
        cbStandardBrushService.page(bzbfPage);
        return R.ok().put("bzbfPage", bzbfPage);
    }

    @GetMapping("/createBzbfZip")
    public R createBzbfZip(){
        try {
            String zipFile = cbStandardBrushService.createBzbfZip();
            fixedInformationDicService.setOne(Constant.ZipStandardBrush.ZIPNAME.getName(), zipFile, new Date());
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("资源包不完整，还需要添加数据");
        } finally {
            System.gc();
            System.runFinalization(); // 强制回收io流
            File zip = new File(Constant.ZipStandardBrush.YSB.getName() + Constant.ZipStandardBrush.ZIPNAME.getName() + ".zip");
            if(zip.exists()){
                // 删除压缩包
                zip.delete();
            }
            // 删除临时目录中下载的临时文件
            File dir = new File(Constant.ZipStandardBrush.PATH.getName());
            if (dir.exists()) {
                if (dir.isDirectory()) {
                    String[] children = dir.list();
                    // 递归删除目录中的子目录下
                    for (int i = 0; i < children.length; i++) {
                        (new File(dir, children[i])).delete();
                    }
                }
                dir.delete();
            }
        }
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    //@RequiresPermissions("sf:cbstandardbrush:info")
    public R info(@PathVariable("id") Long id){
		CbStandardBrushEntity cbStandardBrush = cbStandardBrushService.getById(id);

        return R.ok().put("cbStandardBrush", cbStandardBrush);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    //@RequiresPermissions("sf:cbstandardbrush:save")
    public R save(@RequestBody CbStandardBrushEntity cbStandardBrush){
		cbStandardBrushService.save(cbStandardBrush);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    //@RequiresPermissions("sf:cbstandardbrush:update")
    public R update(@RequestBody CbStandardBrushEntity cbStandardBrush){
		cbStandardBrushService.updateById(cbStandardBrush);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("sf:cbstandardbrush:delete")
    public R delete(@RequestBody Long[] ids){
		cbStandardBrushService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
